package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author Yandel
 * @since 2025-04-21
 */
@Api(tags = "我的课程表相关接口")
@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
public class LearningLessonController {

    private final ILearningLessonService iLearningLessonService;

    @ApiOperation("分页查询我的课表")
    @GetMapping("/page")
    public PageDTO<LearningLessonVO> queryMyLessons(PageQuery query) {
        return iLearningLessonService.queryMyLessons(query);
    }

    @ApiOperation("查询当前正在学习的课程")
    @GetMapping("/now")
    public LearningLessonVO queryMyCurrentLesson() {
        return iLearningLessonService.queryMyCurrentLesson();
    }

    @ApiOperation("根据id查询指定课程的学习状态")
    @GetMapping("/{courseId}")
    public LearningLessonVO queryLearningStatus(
            @ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId) {
        return iLearningLessonService.queryLearningStatus(courseId);
    }

    @ApiOperation("根据id删除指定课程")
    @DeleteMapping("/{courseId}")
    public void deleteLearningLesson(
            @ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId) {
        iLearningLessonService.deleteLearningLesson(null,courseId);
    }


    @ApiOperation("校验当前课程是否已经报名")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(
            @ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId) {
        //如果该用户存在该课程，则返回该课程的id，则标明该课程有效
        return iLearningLessonService.isLessonValid(courseId);
    }




    @ApiOperation("统计课程学习人数")
    @GetMapping("/{courseId}/count")
    public Integer countLearningLessonByCourse(
            @ApiParam(value = "课程id" ,example = "1")
            @PathVariable("courseId") Long courseId) {
        //这里根据课程id查询学习人数，只需要查询课程状态符合要求即可
        // 比如AI课，甲买了且学了    乙买了但没学  丙买了且学完了  丁买了但过期了 ，则这门课一共3个人在学习
        return iLearningLessonService.countLearningLessonByCourse(courseId);
    }


    @ApiOperation("创建学习计划")
    @PostMapping("/plans")
    public void createLearningPlan(@RequestBody @Validated LearningPlanDTO dto) {
        iLearningLessonService.createLearningPlan(dto);
    }

    @ApiOperation("查询我的学习计划")
    @GetMapping("/plans")
    public LearningPlanPageVO queryMyPlans(PageQuery query) {
        return iLearningLessonService.queryMyPlans(query);
    }


}
